<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
require APPPATH."/libraries/FFmpeg.php";
use Aws\S3\S3Client;
class Wangchun extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_activity_activity_model", "table");
        $this->activity = "activity";
        $this->activity_host_name = "activity_host_name";
        $this->activity_select_person = "activity_select_person";
        $this->wangchun_activity = "wangchun_activity";
        $this->type_sub = "type_sub";
    }
////////////////////////////////////////////////////////////网春首页开始/////////////////////////////////////////////////////////////////////////////
    /**
     * 网春首页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        //查询数据
        $res=$this->table->wangchun($arr);
        $data['type']=1;
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $this->layout("activity/wangchun/index",$data);
    }

    /**
     * 新增网春活动
     */
    public function add_wangchun(){
        //查询所有主持人
        $host=$this->table->alls($this->activity_host_name);
        //查询所有拜年对象
        $select_person=$this->table->alls($this->activity_select_person);
        $data['host']=$host['row'];
        $data['select_person']=$select_person['row'];
        $this->layout("activity/wangchun/add_wangchun",$data);
    }

    /**
     * 保存新增网春活动
     */
    public function save_add_wangchun(){
        if(empty($_POST['select_host'])){
            response(-1,"请选择主持人");
        }
        if(empty($_POST['select_person'])){
            response(-1,"请选择拜年对象");
        }
        if(empty($_POST['video_url_url'])){
            response(-1,"请上传视频");
        }
        if(empty($_POST['pic_url'])){
            response(-1,"请上传图片");
        }
        if(empty($_POST['share_pic_url'])){
            response(-1,"请上传分享图片");
        }
        if(empty($_POST['wangchun_big_pic'])){
            response(-1,"请上传大屏图片");
        }
        if(empty($_POST['title'])){
            response(-1,"请添加祝福语");
        }
        if(mb_strlen($_POST['title'])>30){
            response(-1,"祝福语最多30字");
        }
        //获取视屏时长，分辨率
        $ffmpeg = new FFmpeg(false);
        $wh=$ffmpeg->getDetail($_POST['video_url_url']);
        $data['wangchun_width']=$wh['width'];
        $data['wangchun_height']=$wh['height'];
        $data['wangchun_duration']=$wh['duration'];


        //查询主持人信息
        $host_name=$this->table->one($this->activity_host_name,['id'=>$_POST['select_host']]);
        //查询拜年对象信息
        $select_person_name=$this->table->one($this->activity_select_person,['id'=>$_POST['select_person']]);
        //查询加入话题和分类信息
        $fenlei_huati=$this->table->one($this->type_sub,['sub_title'=>"网春大拜年"]);
        $data['wangchun_huati']=$fenlei_huati['id'];
        $data['wangchun_fenlei']=$fenlei_huati['sub_type'];

        $data['wangchun_host']=$_POST['select_host'];
        $data['wangchun_host_name']=$host_name['name'];
        $data['wangchun_select_person_name']=$select_person_name['name'];
        $data['wangchun_select_person']=$_POST['select_person'];
        $data['wangchun_video_url']=$_POST['video_url_url'];
        $data['wangchun_pic']=$_POST['pic_url'];
        $data['wangchun_big_pic']=$_POST['wangchun_big_pic'];
        $data['wangchun_share_pic']=$_POST['share_pic_url'];
        $data['wangchun_title']=$_POST['title'];
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add($this->wangchun_activity,$data);
        if($res){
            response(0,"添加成功",base_url("activity/wangchun/index"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改拜年对象
     */
    public function edit_add_wangchun(){
        $id=$_REQUEST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        //查询所有主持人
        $host=$this->table->alls($this->activity_host_name);
        //查询所有拜年对象
        $select_person=$this->table->alls($this->activity_select_person);
        $res=$this->table->one($this->wangchun_activity,['id'=>$id]);
        $data['data']=$res;
        $data['host']=$host['row'];
        $data['select_person']=$select_person['row'];
        $this->layout("activity/wangchun/edit_add_wangchun",$data);
    }

    /**
     * 保存修改网春活动
     */
    public function save_edit_add_wangchun(){
        if(empty($_POST['select_host'])){
            response(-1,"请选择主持人");
        }
        if(empty($_POST['select_person'])){
            response(-1,"请选择拜年对象");
        }
        if(empty($_POST['video_url_url'])){
            response(-1,"请上传视频");
        }
        if(empty($_POST['pic_url'])){
            response(-1,"请上传图片");
        }
        if(empty($_POST['share_pic_url'])){
            response(-1,"请上传分享图片");
        }
        if(empty($_POST['wangchun_big_pic'])){
            response(-1,"请上传大屏图片");
        }
        if(empty($_POST['title'])){
            response(-1,"请添加祝福语");
        }
        if(mb_strlen($_POST['title'])>30){
            response(-1,"祝福语最多30字");
        }
        if(empty($_POST['id'])){
            response(-1,"缺少参数");
        }
        //获取视屏时长，分辨率
        $ffmpeg = new FFmpeg(false);
        $wh=$ffmpeg->getDetail($_POST['video_url_url']);
        $data['wangchun_width']=$wh['width'];
        $data['wangchun_height']=$wh['height'];
        $data['wangchun_duration']=$wh['duration'];


        //查询主持人信息
        $host_name=$this->table->one($this->activity_host_name,['id'=>$_POST['select_host']]);
        //查询拜年对象信息
        $select_person_name=$this->table->one($this->activity_select_person,['id'=>$_POST['select_person']]);


        $data['wangchun_host']=$_POST['select_host'];
        $data['wangchun_host_name']=$host_name['name'];
        $data['wangchun_select_person_name']=$select_person_name['name'];
        $data['wangchun_select_person']=$_POST['select_person'];
        $data['wangchun_video_url']=$_POST['video_url_url'];
        $data['wangchun_pic']=$_POST['pic_url'];
        $data['wangchun_share_pic']=$_POST['share_pic_url'];
        $data['wangchun_big_pic']=$_POST['wangchun_big_pic'];
        $data['wangchun_title']=$_POST['title'];
        $data['update_time']=date("Y-m-d H:i:s",time());

        $res=$this->table->update($this->wangchun_activity,$data,['id'=>$_POST['id']]);
        if($res){
            response(0,"修改成功",base_url("activity/wangchun/index"));
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 启用
     */
    public function wangchun_up(){
        if(empty($_REQUEST['id'])){
            response(-1,"缺少参数");
        }
        $id=$_REQUEST['id'];
        $data['wangchun_online']=2;
        $res=$this->table->update($this->wangchun_activity,$data,['id'=>$id]);
        if($res){
            response(0,"启用成功");
        }else{
            response(-1,"启用失败");
        }
    }

    /**
     * 停用
     */
    public function wangchun_down(){
        if(empty($_REQUEST['id'])){
            response(-1,"缺少参数");
        }
        $id=$_REQUEST['id'];
        $data['wangchun_online']=1;
        $res=$this->table->update($this->wangchun_activity,$data,['id'=>$id]);
        if($res){
            response(0,"停用成功");
        }else{
            response(-1,"停用失败");
        }
    }
////////////////////////////////////////////////////////////网春首页结束/////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////拜年主持人开始/////////////////////////////////////////////////////////////////////////////
    /**
     * 拜年主持人
     */
    public function host(){
        $res=$this->table->alls($this->activity_host_name,"",1,["add_time"=>"desc"]);
        $data['type']=2;
        $data['data']=$res['row'];

        $this->layout("activity/wangchun/host",$data);
    }

    /**
     * ajax查询有几个人
     */
    public function select_add_host(){
        //先去查询主持人有多少个，最多添加4个
        $all=$this->table->alls($this->activity_host_name,"","id");
        $length=count($all['row']);
        if($length>=4){
            response(-1,"最多添加4个主持人");
        }else{
            response(0,"查询成功");
        }
    }

    /**
     * 添加拜年主持人
     */
    public function save_add_host(){
        //先去查询主持人有多少个，最多添加4个
        $all=$this->table->alls($this->activity_host_name,"","id");
        $length=count($all['row']);
        if($length>=4){
            response(-1,"最多添加4个主持人");
        }
        $name=$_REQUEST['host_name'];
        $host_img_url=$_REQUEST['host_img_url'];
        $host_img_select_url=$_REQUEST['host_img_select_url'];
        if(empty($name)){
            response(-1,"主持人名称不能为空");
        }
        if(empty($host_img_url)){
            response(-1,"主持人头像不能为空");
        }
//        if(empty($host_img_select_url)){
//            response(-1,"主持人选中头像不能为空");
//        }
        if(mb_strlen($name)>3){
            response(-1,"主持人名称不能大于3个字");
        }
        $data['name']=$name;
        $data['host_pic']=$host_img_url;
//        $data['host_pic_select']=$host_img_select_url;
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add($this->activity_host_name,$data);
        if($res){
            response(0,"添加成功");
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改主持人
     */
    public function save_edit_add_host(){
//        var_dump($_REQUEST);exit;
        $name=$_REQUEST['edit_name'];
        $old_name=$_REQUEST['old_name'];
        $id=$_REQUEST['id'];
        $host_img_url=$_REQUEST['edit_host_img_url'];
        $host_img_select_url=$_REQUEST['edit_host_img_select_url'];
        if(empty($name)){
            response(-1,"主持人名称不能为空");
        }
        if(empty($host_img_url)){
            response(-1,"主持人头像不能为空");
        }
//        if(empty($host_img_select_url)){
//            response(-1,"主持人选中头像不能为空");
//        }
        if(mb_strlen($name)>3){
            response(-1,"主持人名称不能大于3个字");
        }
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['name']=$name;
        $data['host_pic']=$host_img_url;
//        $data['host_pic_select']=$host_img_select_url;
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->activity_host_name,$data,['id'=>$id]);
        if($res){
            response(0,"修改成功");
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 查询拜年主持人单条信息
     */
    public function host_one(){
        $id=$_REQUEST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->one($this->activity_host_name,['id'=>$id]);
        if(empty($res)){
            response(-1,"该数据出错");
        }else{
            response(0,"查询成功",$res);
        }
    }

    /**
     * 删除主持人
     */
    public function host_del(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->dels($this->activity_host_name,['id'=>$id]);
        if($res){
            response(0,"删除成功");
        }else{
            response(-1,"删除失败");
        }
    }

/////////////////////////////////////////////////////////////拜年主持人结束////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////拜年对象开始///////////////////////////////////////////////////////////////////////////
    /**
     * 拜年对象
     */
    public function select_person(){
        $res=$this->table->alls($this->activity_select_person,"",1,["add_time"=>"desc"]);
        $data['type']=3;
        $data['data']=$res['row'];
        $this->layout("activity/wangchun/select_person",$data);
    }

    /**
     * 添加拜年对象
     */
    public function save_add_select_person(){
        //先去查询拜年对象有多少个，最多添加10个
        $all=$this->table->alls($this->activity_select_person,"","id");
        $length=count($all['row']);
        if($length>=8){
            response(-1,"最多添加8个拜年对象");
        }
        $name=$_REQUEST['host_name'];
        if(empty($name)){
            response(-1,"拜年对象名称不能为空");
        }
        if(mb_strlen($name)>4){
            response(-1,"拜年对象名称不能大于4个字");
        }
        $data['name']=$name;
        $data['add_time']=date("Y-m-d H:i:s",time());
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->add($this->activity_select_person,$data);
        if($res){
            response(0,"添加成功");
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改拜年对象
     */
    public function save_edit_select_person(){
        $name=$_REQUEST['edit_name'];
        $old_name=$_REQUEST['old_name'];
        $id=$_REQUEST['id'];
        if(empty($name)){
            response(-1,"拜年对象名称不能为空");
        }
        if(mb_strlen($name)>4){
            response(-1,"拜年对象名称不能大于4个字");
        }
        if($old_name==$name){
            response(-1,"原名称与修改名称不能相同");
        }
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $data['name']=$name;
        $data['update_time']=date("Y-m-d H:i:s",time());
        $res=$this->table->update($this->activity_select_person,$data,['id'=>$id]);
        if($res){
            response(0,"修改成功");
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 删除拜年对象
     */
    public function select_person_del(){
        $id=$_POST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        $res=$this->table->dels($this->activity_select_person,['id'=>$id]);
        if($res){
            response(0,"删除成功");
        }else{
            response(-1,"删除失败");
        }
    }
////////////////////////////////////////////////////////////////拜年对象结束/////////////////////////////////////////////////////////////////////////
///

    /**
     * 返回上传地址
     */
    public function upload_dir($type="image",$suffix="png"){
        //解决跨域问题
        $this->config->load("upload");
        $uploadConfigs = config_item("upload");
        //创建上传目录，配置上传
        $date1=date("Y",time());
        $date2=date("m",time());
        $date3=date("d",time());
        $dir="/".$date1."/".$date2."/".$date3;
        $uploadpath1=$uploadConfigs[$type]['uploadpath'];
        $uploadpath2=$uploadConfigs[$type]['savehttp'];
        $key1=rand(1000,10000);
        $key2=rand(10000,20000);
        $key3=rand();
        $file_name=$key1.$key2.$key3;
        //这是服务器上的上传地址
        $uploadpath['uploadpath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
        if($type=="video"){
            mkdir($uploadpath1.$dir."/".$file_name,0777,true);
        }
        //这是网络访问地址
        $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
        return $uploadpath;
    }

    /**
     * 返回临时存储目录
     */
    public function tmp_dir($type="test"){
        //解决跨域问题
        $this->config->load("upload");
        $uploadConfigs = config_item("upload");
        //创建上传目录，配置上传
        $date1=date("Y",time());
        $date2=date("m",time());
        $date3=date("d",time());
        $dir="/".$date1."/".$date2."/".$date3;
        $uploadpath1=$uploadConfigs[$type]['tmppath'];
        //这是服务器上的上传地址
        $uploadpath=$uploadpath1.$dir;
        return $uploadpath;
    }

}